import { ResourcesLoader } from "./ResourcesLoader";


const { ccclass, property } = cc._decorator;

export type TableItemTypeData = {
	/**
	 * 房间等级
	 */
	RoomLevel: number;
};

@ccclass
export default class TableItemTypePrefab extends cc.Component {
	@property(cc.Sprite)
	picSprite: cc.Sprite = null;


	@property(cc.SpriteFrame)
	tableSpriteFrame = [];

	private _data: TableItemTypeData = null;

	onLoad() {
		this.node.opacity = 0;
	}

	/**
	 * 绑定数据
	 */
	bindData(data: TableItemTypeData) {
		this._data = data;
	}

	/**
	 * 绑定数据
	 */
	getData() {
		return this._data;
	}
	/**
	 * 本Item进入ScrollView的时候回调
	 */
	onEnterSrcollView() {
		this.node.opacity = 255;
		this._loadAndShowPic();
	}

	/**
	 * 本Item离开ScrollView的时候回调
	 */
	onExitScrollView() {
		this.node.opacity = 0;
	}

	/**
	 * 加载并展示图片
	 */
	private async _loadAndShowPic() {
		this._showPlaceHolder();

		// 模拟延迟一段时间后再加载成功
		await new Promise((resolve, reject) => {
			setTimeout(() => {
				resolve();
			}, 160);
		});
		this.picSprite.spriteFrame = this.tableSpriteFrame[this._data.RoomLevel]
		this._hidePlaceHolder();
	}

	private _showPlaceHolder() {
	}

	private _hidePlaceHolder() {
	}
}
